package com.vx.service.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.vx.service.IBorrowService;
import com.vx.vo.BorrowVO;

@Service("borrowService")
public class BorrowServiceImpl implements IBorrowService {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public List<BorrowVO> findBorrowList(String userCode) {
		List<BorrowVO> list=jdbcTemplate.query("select * from s_borrow where usercode=? ", new Object[]{userCode},new BorrowMapper());
		return list;
	}

	@Override
	public BorrowVO findBorrowById(String borrowId) {
		BorrowVO borrow=jdbcTemplate.queryForObject("select * from s_borrow where id=? ", new Object[]{borrowId},new BorrowMapper());
		return borrow;
	}

	@Override
	public void saveBorrow(BorrowVO borrow) {
		jdbcTemplate.update("update  s_borrow set status=? where id=? ", new Object[]{borrow.getStatus(),borrow.getId()});
	}

	@Override
	public void deleteBorrow(String borrowId) {
		jdbcTemplate.update("delete from s_borrow where id=? ", borrowId);
		
	}
	
//	@Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.READ_COMMITTED) 
	@Override
	public void addBorrow(BorrowVO borrow) throws Exception {
		String sql="insert into s_borrow(title,filesyscode,usercode,time,status,flowKey) values(?,?,?,?,?,?)";
		jdbcTemplate.update(sql, new Object[]{borrow.getTitle(),borrow.getFilesyscode(),borrow.getUsercode(),borrow.getTime(),borrow.getStatus(),borrow.getFlowKey()});
	}

}

class BorrowMapper implements RowMapper{

	@Override
	public Object mapRow(ResultSet rs, int num) throws SQLException {
		BorrowVO borrow=new BorrowVO();
		borrow.setId(rs.getLong("id"));
		borrow.setTitle(rs.getString("title"));
		borrow.setUsercode(rs.getString("usercode"));
		borrow.setFilesyscode(rs.getString("filesyscode"));
		borrow.setTime(rs.getDate("time"));
		borrow.setStatus(rs.getInt("status"));
		borrow.setFlowKey(rs.getString("flowKey"));
		return borrow;
		
	}
}